package com.boful.common.file.utils;

import java.io.File;
import java.lang.reflect.Field;

public class FileType {
	public static final int TYPE_AVI = 0xFF00001;
	public static final int TYPE_MP4 = 0xFF00002;
	public static final int TYPE_RMVB = 0xFF00003;
	public static final int TYPE_3GP = 0xFF00004;
	public static final int TYPE_INVALID = -1;

	// 视频格式
	public static final String[] VIDEO_TYPES = { "avi", "mp4", "3gp", "rmvb",
			"wmv", "flv", "mpeg", "mkv", "rm", "dat", "vob", "mov","tp","mpg","ts","asf","f4v","mts"};
	// 文档格式
	public static final String[] DOCUMENT_TYPE = { "doc", "docx", "ppt",
			"pptx", "xls", "xlsx", "txt" };

	// 音频格式
	public static final String[] AUDIO_TYPES = { "mp3", "wma", "wav", "mod",
			"cd", "mid" };

	// 图片格式
	public static final String[] IMAGE_TYPES = { "jpg", "png", "gif", "bmp","jpeg"};

	public static int getFileTypeCode(String fileType) {
		Field[] fields = FileType.class.getDeclaredFields();
		Field field = null;
		for (Field tmp : fields) {
			if (tmp.getName().equals("TYPE_" + fileType.toUpperCase())) {
				field = tmp;
				break;
			}
		}
		if (field == null) {
			return TYPE_INVALID;
		}
		int typeCode = TYPE_INVALID;
		try {
			typeCode = field.getInt(new FileType());
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return typeCode;
	}

	public static String getFileType(File file) {
		String[] fileNames = file.getName().split("\\.");
		return fileNames[fileNames.length - 1];
	}

	public static String getFileType(int typeCode) {
		Field[] fields = FileType.class.getDeclaredFields();
		for (Field tmp : fields) {
			try {
				int tmpCode = tmp.getInt(new FileType());
				if (tmpCode == typeCode) {
					String typeName = tmp.getName().substring(5);
					if (typeName.equals("INVALID")) {
						return null;
					} else {
						return typeName.toLowerCase();
					}
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static boolean isVideo(String fileName) {
		String sufix = FileUtils.getFileSufix(fileName);
		for (String type : VIDEO_TYPES) {
			if (sufix.equalsIgnoreCase(type)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isDocument(String fileName) {
		String sufix = FileUtils.getFileSufix(fileName);
		for (String type : DOCUMENT_TYPE) {
			if (sufix.equalsIgnoreCase(type)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isAudio(String fileName) {
		String sufix = FileUtils.getFileSufix(fileName);
		for (String type : AUDIO_TYPES) {
			if (sufix.equalsIgnoreCase(type)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isImage(String fileName) {
		String sufix = FileUtils.getFileSufix(fileName);
		for (String type : IMAGE_TYPES) {
			if (sufix.equalsIgnoreCase(type)) {
				return true;
			}
		}
		return false;
	}

}
